﻿// MinMathParser to demonstrate very simplyfied expression parser
// -------------------------------------------------------------------
// License: MS-RL License
// Version: 1.0.0.0
// Initial Development: Prosodev - Software Company / September 2013 / V 1.0.0.0

namespace MinMathParser
{
    using System.Collections.Generic;

    /// <summary>
    ///     Very simplified tokenizer.
    /// </summary>
    public static class Tokenizer
    {
        #region Public Methods and Operators

        public static bool IsAddOrSub(this string argument)
        {
            return argument == "+" || argument == "-";
        }

        public static bool IsCloseBracket(this string argument)
        {
            return argument == ")";
        }

        public static bool IsLastToken(this List<string> token, int index)
        {
            return index == token.Count - 1;
        }

        public static bool IsMulOrDiv(this string argument)
        {
            return argument == "*" || argument == "/";
        }

        public static bool IsNum(this string argument)
        {
            int val;
            return int.TryParse(argument, out val);
        }

        public static bool IsOpenBracket(this string argument)
        {
            return argument == "(";
        }

        public static List<string> TokenizeByNonDigits(this string argument)
        {
            var tokens = new List<string>();
            var currentToken = string.Empty;
            while (argument.Length > 0)
            {
                if (!char.IsDigit(argument[0]))
                {
                    if (!string.IsNullOrEmpty(currentToken))
                    {
                        tokens.Add(currentToken);
                        currentToken = string.Empty;
                    }
                    tokens.Add(argument[0] + string.Empty);
                }
                else
                {
                    currentToken += argument[0];
                }

                if (argument.Length == 1)
                {
                    if (!string.IsNullOrEmpty(currentToken))
                    {
                        tokens.Add(currentToken);
                    }
                }

                argument = argument.Substring(1);
            }

            return tokens;
        }

        #endregion
    }
}